/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelOrdenMedica.java
 *
 * Created on 27-jul-2012, 1:09:20
 */
package co.edu.eam.clinica.vista.gui.consulta;

import co.edu.eam.clinica.vista.controladores.consulta.controladorConsultaClinica;
import co.edu.eam.clinica.vista.gui.common.OncologosTableModel;
import co.edu.eam.clinica.vista.gui.common.PanelOncologos;
import co.edu.eam.persitencia.entidades.Articulo;
import co.edu.eam.persitencia.entidades.Consulta;
import co.edu.eam.persitencia.entidades.DetalleFormulaMedica;
import co.edu.eam.persitencia.entidades.Medicamento;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Camilo Andres
 */
public class PanelOrdenMedica extends PanelOncologos {
controladorConsultaClinica control;
    public PanelOrdenMedica(controladorConsultaClinica control) {
        initComponents();
        this.control=control;
        llenarMedicamento();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        bBuscarDiagnostico = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tablaProc = new javax.swing.JTable();
        bEliminarProc = new javax.swing.JButton();
        bAgregar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        jLabel9.setText("medicamento");

        bBuscarDiagnostico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/buscar-buscar-ampliar-icono-4271-32.png"))); // NOI18N
        bBuscarDiagnostico.setText("aceptar");
        bBuscarDiagnostico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarDiagnosticoActionPerformed(evt);
            }
        });

        tablaProc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "id del medicamento", "cantidad", "preescripcion"
            }
        ));
        jScrollPane3.setViewportView(tablaProc);

        bEliminarProc.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/cancelar.jpg"))); // NOI18N
        bEliminarProc.setText("Eliminar");
        bEliminarProc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEliminarProcActionPerformed(evt);
            }
        });

        bAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/Add.png"))); // NOI18N
        bAgregar.setText("Agregar");
        bAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAgregarActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel1.setText("Prescripcion");

        jLabel2.setText("Cantidad");

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(bEliminarProc)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bBuscarDiagnostico))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 399, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(bAgregar)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTextField1)
                                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE))))))
                        .addGap(0, 170, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(bAgregar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bEliminarProc)
                    .addComponent(bBuscarDiagnostico))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bBuscarDiagnosticoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarDiagnosticoActionPerformed
           
        
        
}//GEN-LAST:event_bBuscarDiagnosticoActionPerformed

    private void bEliminarProcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEliminarProcActionPerformed
//eliminar el medixcamento de la lista
        List<DetalleFormulaMedica>listaMedi=control.getMeds();
        
        DefaultTableModel modelo=(DefaultTableModel) tablaProc.getModel();
        int id=tablaProc.getSelectedRow();
        long idMed=(long) tablaProc.getValueAt(id, 0);
        
        control.eliminarMed(idMed);
        
        this.tablaProc.setModel(modelo);
        
}//GEN-LAST:event_bEliminarProcActionPerformed

    private void bAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAgregarActionPerformed
        
        try {
            Articulo art= (Articulo) jComboBox1.getSelectedItem();
            String preesscripcion= jTextArea1.getText();
            int cantidad=Integer.parseInt(jTextField1.getText());
            
            control.agregarMed(art, cantidad, preesscripcion);
            
        DefaultTableModel modelo = (DefaultTableModel) tablaProc.getModel();
        List<DetalleFormulaMedica>lista=control.getMeds();
            
            modelo.setNumRows(lista.size());
            
            for (int i = 0; i < lista.size(); i++) {
                
                DetalleFormulaMedica dfm =  (DetalleFormulaMedica) lista.get(i);
                
                modelo.setValueAt(dfm.getId(), i, 0);
                modelo.setValueAt(dfm.getCantidad(), i, 1);
                modelo.setValueAt(dfm.getPrescripcion(), i, 2);
                
                this.tablaProc.setModel(modelo);
        } 
        }catch (Exception ex) {
            Logger.getLogger(PanelOrdenMedica.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_bAgregarActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAgregar;
    private javax.swing.JButton bBuscarDiagnostico;
    private javax.swing.JButton bEliminarProc;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTable tablaProc;
    // End of variables declaration//GEN-END:variables

    private void llenarMedicamento() {
        try {
            List<Medicamento>lista=control.listarMedicamento();
            for (int i = 0; i < lista.size(); i++) {
                Medicamento medicamento=lista.get(i);
                jComboBox1.addItem(medicamento);
            }
        } catch (Exception ex) {
            Logger.getLogger(PanelOrdenMedica.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
